﻿using Acme.BookStore.Enum;
using Acme.BookStore.Lodgings;
using Acme.BookStore.RBAC;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;


namespace Acme.BookStore.Order
{

    [ApiExplorerSettings(GroupName = "v2")]
    
    public class Orderservices: ApplicationService, IOrderservices
    {
        IRepository<Reservations> _Reservationsrepository;

        IRepository<UserClients> _UserReservationsrepository;

        IRepository<RoomNumber> _RoomNumberReservationsrepository;

        IRepository<RoomType> _RoomTypeReservationsrepository;
       

        public Orderservices(IRepository<Reservations> reservationsrepository, IRepository<UserClients> userReservationsrepository, IRepository<RoomNumber> roomNumberReservationsrepository, IRepository<RoomType> roomTypeReservationsrepository)
        {
            _Reservationsrepository = reservationsrepository;
            _UserReservationsrepository = userReservationsrepository;
            _RoomNumberReservationsrepository = roomNumberReservationsrepository;
            _RoomTypeReservationsrepository = roomTypeReservationsrepository;
           
        }
        /// <summary>
        ///订单显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="OrderType"></param>
        /// <param name="PaidStatus"></param>
        /// <param name="CheckIntime"></param>
        /// <param name="CheckOuttime"></param>
        /// <param name="FullStatus"></param>
        /// <param name="RoomTypeId"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet("/api/app/orderservices/orders")]

        public async Task<ApiResult> GetOrders( OrderTypeEnum OrderType, PaidTypeEnum PaidStatus,DateTime? CheckIntime,DateTime? CheckOuttime, AllStatusEnum FullStatus, string RoomName, Guid? UserId ,string UserName, int PageIndex, int PageSize)
        {
            var user = await _UserReservationsrepository.GetListAsync();
            var roomNumber = await _RoomNumberReservationsrepository.GetListAsync();
            var roomType = await _RoomTypeReservationsrepository.GetListAsync();
            var reservations = await _Reservationsrepository.GetListAsync();


           
           var list = from a in user
                           join b in reservations on a.Id equals b.UserId
                           join c in roomType on b.RoomTypeId equals c.Id
                           join d in roomNumber on b.RoomId equals d.Id
                           select new OrderDTO()
                           {

                               UserName = a.UserName,
                               UserPwd = a.UserPwd,
                               UserNickName = a.UserNickName,

                               UserId = a.Id,
                               CheckIntime = b.CheckIntime,
                               CheckOuttime = b.CheckOuttime,
                               OutofPocketamount = b.OutofPocketamount,
                               RoomTypeId = c.Id,
                               OrderType = b.OrderType,
                               FullStatus = b.FullStatus,
                               PaidStatus = b.PaidStatus,
                               RoomId = d.Id,
                               ReservationsId = b.Id,
                               RoomName = c.RoomName,
                               Id = d.Id,

                           };

                //if (!string.IsNullOrEmpty(UserName))
                //{
                //    list = list.Where(x => x.UserName.Contains(UserName) || x.User_Phone.Contains(UserName) || x.User_Email.Contains(UserName));
                //}

                ////根据时间查询
                //if (CheckIntime != null && CheckOuttime == null)
                //{
                //    list = list.Where(x => x.CheckIntime >= CheckIntime);
                //}
                //if (CheckOuttime != null && CheckIntime == null)
                //{
                //    list = list.Where(x => x.CheckOuttime <= CheckOuttime);
                //}

                //if (CheckIntime != null && CheckOuttime != null)
                //{
                //    list = list.Where(x => CheckIntime >= CheckIntime && x.CheckOuttime <= CheckOuttime);
                //}

                //if (OrderType != 0)
                //{
                //    list = list.Where(x => x.OrderType == OrderType);
                //}

                //if (PaidStatus != 0)
                //{
                //    list = list.Where(x => x.PaidStatus == PaidStatus);
                //}

                //if (FullStatus != 0)
                //{
                //    list = list.Where(x => x.FullStatus == FullStatus);
                //}

                //if (RoomName != null)
                //{
                //    list = list.Where(x => x.RoomName == RoomName);
                //}

                var page = new APiPaging()
                {
                    TotalCount = list.Count(),
                    TotalPage = Convert.ToInt32(Math.Ceiling(list.Count() / Convert.ToDouble(PageSize))),
                    Data = list.OrderBy(x => x.CheckIntime).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList()
                };
                return ApiResult.Ok(page);

            }




            /// <summary>
            /// 添加订单
            /// </summary>
            /// <param name="orderAddDTO"></param>
            /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> CreateOrder(OrderAddDTO orderAddDTO)
        {
            var list = ObjectMapper.Map<OrderAddDTO, Reservations>(orderAddDTO);
            await  _Reservationsrepository.InsertAsync(list);
            return ApiResult.Ok(list);


        }
        /// <summary>
        /// 绑定下拉框
        /// </summary>
        /// <param name="RoomName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetText()
        {
            var roomNumber = await _RoomTypeReservationsrepository.GetListAsync();

            return ApiResult.Ok(roomNumber);
        }

        /// <summary>
        /// 支付订单查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult> GetMemberCode(Guid id)
        {
           var  list =  await _Reservationsrepository.GetAsync(x=>x.Id==id);
            return ApiResult.Ok(list);
        }

    }
}
